<?php
namespace app\admin\controller\nursinguser;;

use app\admin\model\NursingOrder;
use app\admin\model\PayOrder;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use app\admin\model\publicuse\PublicUse;
use smartybuilder\libs\Quickbuilder;
use think\App;
/**
 * @ControllerAnnotation(title="退款订单")
 * Class Refundorder
 */
class Refundorder extends AdminController
{
    use \app\admin\traits\Curd;
     protected $allowModifyFields = [
    ];
    protected $noExportFields = [
        'id',
        'name',
        'sort',
        'uid',
        'order_num',
        'money',
        'reason',
        'pay_order_id',
        'back_reason',
        'phone',
        'create_time',
        'update_time',
        'status',
        'delete_time',
    ];
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\admin\model\RefundOrder();
        $array = PublicUse::getConfigDir(__CLASS__);
        if(is_file(__DIR__.DIRECTORY_SEPARATOR.'field'.DIRECTORY_SEPARATOR.$array['class'].'.txt')){
           $this->AllData = json_decode(file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'field'.DIRECTORY_SEPARATOR.$array['class'].'.txt'),true);
        }
    }
    /**
     * @NodeAnotation(title="审核失败")
     */
    public function shenheshibai(){
        $id = $this->request->param('id');
        $data = $this->request->param();
        $order = $this->model->find($id);
        if(isset($data['back_reason'])){
           $bool = $order->save(['status'=>2,'back_reason'=>$data['back_reason'],'update_time'=>time()]);
           if(!$bool){
               return $this->error('修改失败');
           }
           $pay_order = PayOrder::where('status',1)
               ->where('order_num',$order->getData('order_num'))
               ->find();
           $nursing_order = NursingOrder::where('id',$pay_order->getData('nursing_order_id'))
               ->find();
            $pay_order->state = 6;
            $nursing_order->pay_state = 6;
            $pay_order->save(['update_time'=>time()]);
            $nursing_order->save(['update_time'=>time()]);
            return $this->success('修改成功');
        }else{
            return Quickbuilder::makeForm()
                ->setAction()
                ->set_up_form('none','','id',$id)
                ->set_up_form('textarea','审核失败原因','back_reason',$order->getData('back_reason'))
                ->set_button('submit','提交')
                ->fetch();
        }
    }
    /**
     * @NodeAnotation(title="退款成功")
     */
    public function tuikuanchengg(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('请输入要成功的订单');
        }
        $order = $this->model->find($id);
        if(empty($order)){
            return $this->error('订单不存在');
        }
        $bool = $order->save(['status'=>4,'back_reason'=>'','update_time'=>time()]);
        if(!$bool){
            return $this->error('修改失败');
        }
        $pay_order = PayOrder::where('status',1)
            ->where('order_num',$order->getData('order_num'))
            ->find();
        $nursing_order = NursingOrder::where('id',$pay_order->getData('nursing_order_id'))
            ->find();
        $pay_order->state = 5;
        $nursing_order->pay_state = 5;
        $pay_order->save(['update_time'=>time()]);
        $nursing_order->save(['update_time'=>time()]);
        return $this->success('修改成功');
    }
    /**
     * @NodeAnotation(title="在线退款")
     */
    public function zaixiantui(){
        return $this->error('在线退款暂时没有实现');
    }
}